package com.zyt.state.solution_table_lookup;

import com.zyt.state.State;

/**
 * 方案一：使用分支流程法
 */
public class MarioStateMachineTwo {
    

    private State[][] stableTable = {
        {State.SUPER, State.CAPE, State.FIRE, State.SMALL},
        {State.SUPER, State.CAPE, State.FIRE, State.SMALL},
        {State.CAPE, State.CAPE, State.CAPE, State.SMALL},
        {State.FIRE, State.FIRE, State.FIRE, State.SMALL},
    };

    private int[][] scoreTable = {
        {100, 200, 300, 0},
        {0, 200, 300, -100},
        {0, 0, 0, -200},
        {0, 0, 0, -300},
    };

    private State currentState;
    private int score;

    public State getCurrentState() {
        return currentState;
    }

    public MarioStateMachineTwo(State state) {
        this.currentState = state;
        this.score = 0;
    }

    public int getScore() {
        return score;
    }

    public void obtainCape() {
        evaluateEvent(Event.CAPE);
    }

    public void obtainFlower() {
        evaluateEvent(Event.FLOWER);
    }

    public void obtainMushRoom() {
        evaluateEvent(Event.MUSHROOM);
    }

    public void meetMonster() {
       evaluateEvent(Event.MONSTER);
    }

    private void evaluateEvent(Event event) {
        int curStateValue = this.currentState.getValue();
        this.currentState = this.stableTable[curStateValue][event.getValue()];
        int scoreDelta = this.scoreTable[curStateValue][event.getValue()]; 
        this.score += scoreDelta;
    }
}
